package com.lzb.permanentcalendar.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.permanentcalendar.BaseAc;
import com.lzb.permanentcalendar.HomeActivity;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.bean.ScheduleBean;
import com.lzb.permanentcalendar.databinding.ActivityScheduleDetailBinding;
import com.lzb.permanentcalendar.utils.AppSpUtils;

import java.text.SimpleDateFormat;
import java.util.List;

public class ScheduleDetailActivity extends BaseAc<ActivityScheduleDetailBinding> implements View.OnClickListener {

    public static ScheduleBean scheduleBean;

    @Override
    protected int onCreate() {
        return R.layout.activity_schedule_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivScheduleDetailBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvEdit.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvEdit) {//编辑
            AddScheduleActivity.scheduleBean = scheduleBean;
            startActivity(AddScheduleActivity.class);
        } else if (id == R.id.tvDelete) {//删除
            List<ScheduleBean> listSp = AppSpUtils.getScheduleList();
            for (int i = 0; i < listSp.size(); i++) {
                ScheduleBean bean = listSp.get(i);
                if (bean.getId().equals(scheduleBean.getId())) {
                    listSp.remove(i);
                }
            }
            AppSpUtils.saveScheduleList(listSp);
            ToastUtils.showShort(getString(R.string.delete_suc));
            startActivity(HomeActivity.class);
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvScheduleType.setText(scheduleBean.getType());
        if (scheduleBean.isAllDay()) {
            mDataBinding.tvScheduleTime.setText(TimeUtils.millis2String(scheduleBean.getStartTime(), new SimpleDateFormat("yyyy年MM月dd日 EEE")) + " " + getString(R.string.all_day));
        } else {
            mDataBinding.tvScheduleTime.setText(TimeUtils.millis2String(scheduleBean.getStartTime(), new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm")));
        }
        mDataBinding.tvScheduleTitle.setText(scheduleBean.getTitle());
        mDataBinding.tvScheduleRemark.setText(scheduleBean.getRemark());
        mDataBinding.tvRemind.setText(scheduleBean.getRemindType());
        mDataBinding.tvRepeat.setText(scheduleBean.getRepeatType());
    }
}